package com.zwy.fornote.controller;

import com.zwy.fornote.entity.req.NewAnswerReq;
import com.zwy.fornote.entity.vo.AnswerVO;
import com.zwy.fornote.service.AnswerService;
import com.zwy.fornote.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/answer")
public class AnswerController {
    @Autowired
    AnswerService answerService;

    @PostMapping("/createNewAnswer")
    public AjaxResult createNewAnswer(@RequestBody NewAnswerReq req)
    {
        int res = answerService.answerQuestion(req);
        if(res>0)
        {
            return AjaxResult.ok("回答成功");
        }
        return AjaxResult.fail(-1,"回答失败");

    }

    @GetMapping("/getAnswerByQuesId")
    public AjaxResult getAnswerByQuesId( int quesId)
    {
        try {
            List<AnswerVO> answerByQuesId = answerService.getAnswerByQuesId(quesId);
            if(answerByQuesId!=null)
            {
                return AjaxResult.ok(answerByQuesId);
            }else
            {
                return AjaxResult.fail(-1,"暂无评论！");
            }
        }catch (Exception e)
        {
            return AjaxResult.fail(-1,e.getMessage());
        }

    }


}
